// 这个脚本用于下载项目所需的图片资源
const fs = require('fs');
const https = require('https');
const path = require('path');

// 创建目录函数
function ensureDirectoryExists(dirPath) {
  if (!fs.existsSync(dirPath)) {
    fs.mkdirSync(dirPath, { recursive: true });
  }
}

// 下载图片函数
function downloadImage(url, filepath) {
  return new Promise((resolve, reject) => {
    const dirPath = path.dirname(filepath);
    ensureDirectoryExists(dirPath);

    const file = fs.createWriteStream(filepath);
    
    https.get(url, (response) => {
      response.pipe(file);
      
      file.on('finish', () => {
        file.close();
        console.log(`已下载: ${filepath}`);
        resolve();
      });
    }).on('error', (err) => {
      fs.unlink(filepath, () => {}); // 删除不完整的文件
      console.error(`下载失败: ${filepath} - ${err.message}`);
      reject(err);
    });
  });
}

// 要下载的图片列表
const imagesToDownload = [
  // 底部导航栏图标 - 使用实际图标
  { 
    url: 'https://img.icons8.com/sf-regular/48/FF6B81/home.png', 
    path: 'images/tabbar/home.png' 
  },
  { 
    url: 'https://img.icons8.com/sf-regular-filled/48/FF6B81/home.png', 
    path: 'images/tabbar/home-active.png' 
  },
  { 
    url: 'https://img.icons8.com/sf-regular/48/FF6B81/categorize.png', 
    path: 'images/tabbar/category.png' 
  },
  { 
    url: 'https://img.icons8.com/sf-regular-filled/48/FF6B81/categorize.png', 
    path: 'images/tabbar/category-active.png' 
  },
  { 
    url: 'https://img.icons8.com/sf-regular/48/FF6B81/book.png', 
    path: 'images/tabbar/knowledge.png' 
  },
  { 
    url: 'https://img.icons8.com/sf-regular-filled/48/FF6B81/book.png', 
    path: 'images/tabbar/knowledge-active.png' 
  },
  { 
    url: 'https://img.icons8.com/sf-regular/48/FF6B81/shopping-cart.png', 
    path: 'images/tabbar/cart.png' 
  },
  { 
    url: 'https://img.icons8.com/sf-regular-filled/48/FF6B81/shopping-cart.png', 
    path: 'images/tabbar/cart-active.png' 
  },
  { 
    url: 'https://img.icons8.com/sf-regular/48/FF6B81/user.png', 
    path: 'images/tabbar/profile.png' 
  },
  { 
    url: 'https://img.icons8.com/sf-regular-filled/48/FF6B81/user.png', 
    path: 'images/tabbar/profile-active.png' 
  },
  
  // 首页分类图标 - 根据代码中实际使用的路径
  { 
    url: 'https://img.icons8.com/pastel-glyph/64/666666/dress.png', 
    path: 'images/icons/category1.png' 
  },
  { 
    url: 'https://img.icons8.com/pastel-glyph/64/666666/t-shirt.png', 
    path: 'images/icons/category2.png' 
  },
  { 
    url: 'https://img.icons8.com/pastel-glyph/64/666666/childrens-clothes.png', 
    path: 'images/icons/category3.png' 
  },
  { 
    url: 'https://img.icons8.com/pastel-glyph/64/666666/running-shoe.png', 
    path: 'images/icons/category4.png' 
  },
  { 
    url: 'https://img.icons8.com/pastel-glyph/64/666666/necklace.png', 
    path: 'images/icons/category5.png' 
  },
  { 
    url: 'https://img.icons8.com/pastel-glyph/64/666666/lipstick.png', 
    path: 'images/icons/category6.png' 
  },
  { 
    url: 'https://img.icons8.com/pastel-glyph/64/666666/sofa.png', 
    path: 'images/icons/category7.png' 
  },
  { 
    url: 'https://img.icons8.com/pastel-glyph/64/666666/smartphone-tablet.png', 
    path: 'images/icons/category8.png' 
  },
  { 
    url: 'https://img.icons8.com/pastel-glyph/64/666666/restaurant.png', 
    path: 'images/icons/category9.png' 
  },
  { 
    url: 'https://img.icons8.com/pastel-glyph/64/666666/categorize.png', 
    path: 'images/icons/category10.png' 
  },
  { 
    url: 'https://img.icons8.com/pastel-glyph/64/666666/menu-rounded.png', 
    path: 'images/icons/all.png' 
  },
  
  // 美妆分类图标
  { 
    url: 'https://img.icons8.com/pastel-glyph/64/666666/face-wash.png', 
    path: 'images/category/skincare.png' 
  },
  { 
    url: 'https://img.icons8.com/pastel-glyph/64/666666/foundation-makeup.png', 
    path: 'images/category/makeup.png' 
  },
  { 
    url: 'https://img.icons8.com/pastel-glyph/64/666666/perfume.png', 
    path: 'images/category/perfume.png' 
  },
  { 
    url: 'https://img.icons8.com/pastel-glyph/64/666666/cream.png', 
    path: 'images/category/body.png' 
  },
  { 
    url: 'https://img.icons8.com/pastel-glyph/64/666666/mascara.png', 
    path: 'images/category/tools.png' 
  },
  { 
    url: 'https://img.icons8.com/pastel-glyph/64/666666/gift.png', 
    path: 'images/category/sets.png' 
  },
  
  // 搜索和顶部图标
  { 
    url: 'https://img.icons8.com/pastel-glyph/64/666666/search.png', 
    path: 'images/icons/search.png' 
  },
  { 
    url: 'https://img.icons8.com/pastel-glyph/64/666666/up.png', 
    path: 'images/icons/top.png' 
  },
  { 
    url: 'https://img.icons8.com/pastel-glyph/64/666666/empty-box.png', 
    path: 'images/icons/empty.png' 
  },
  
  // 其他UI元素
  { 
    url: 'https://images.unsplash.com/photo-1544717305-2782549b5136?ixlib=rb-1.2.1&auto=format&fit=crop&w=1200&q=80', 
    path: 'images/default_banner.png' 
  },
  { 
    url: 'https://img.icons8.com/pastel-glyph/128/999999/shopping-cart.png', 
    path: 'images/cart-empty.png' 
  },
  
  // 产品图片示例
  { 
    url: 'https://images.unsplash.com/photo-1631730486572-5183924ae161?ixlib=rb-1.2.1&auto=format&fit=crop&w=400&q=80', 
    path: 'images/products/cleanser1.jpg' 
  },
  { 
    url: 'https://images.unsplash.com/photo-1620916566398-39f1143ab7be?ixlib=rb-1.2.1&auto=format&fit=crop&w=400&q=80', 
    path: 'images/products/cleanser2.jpg' 
  },
  { 
    url: 'https://images.unsplash.com/photo-1611080846927-de6339abacd6?ixlib=rb-1.2.1&auto=format&fit=crop&w=400&q=80', 
    path: 'images/products/toner1.jpg' 
  },
  { 
    url: 'https://images.unsplash.com/photo-1601049541289-9b1b7bbbfe19?ixlib=rb-1.2.1&auto=format&fit=crop&w=400&q=80', 
    path: 'images/products/toner2.jpg' 
  },
  
  // 知识文章图片
  { 
    url: 'https://images.unsplash.com/photo-1571875257727-256c39da42af?ixlib=rb-1.2.1&auto=format&fit=crop&w=600&q=80', 
    path: 'images/knowledge/skin1.jpg' 
  },
  { 
    url: 'https://images.unsplash.com/photo-1522335789203-aabd1fc54bc9?ixlib=rb-1.2.1&auto=format&fit=crop&w=600&q=80', 
    path: 'images/knowledge/makeup1.jpg' 
  },
  { 
    url: 'https://images.unsplash.com/photo-1563170351-be82bc888aa4?ixlib=rb-1.2.1&auto=format&fit=crop&w=600&q=80', 
    path: 'images/knowledge/perfume1.jpg' 
  },
  { 
    url: 'https://images.unsplash.com/photo-1571646034647-52e6ea84b617?ixlib=rb-1.2.1&auto=format&fit=crop&w=600&q=80', 
    path: 'images/knowledge/body1.jpg' 
  }
];

// 下载所有图片
async function downloadAllImages() {
  console.log('开始下载图片...');
  
  const promises = imagesToDownload.map(image => 
    downloadImage(image.url, image.path)
  );
  
  try {
    await Promise.all(promises);
    console.log('所有图片下载完成！');
  } catch (error) {
    console.error('下载过程中出错:', error);
  }
}

// 执行下载
downloadAllImages(); 